﻿using FPSO.HMI.Controls;
using FPSO.HMI.Utils;
using FPSO.Models.Constant;
using FPSO.Models.Domain;
using FPSO.Services;
using FPSO.Services.Impl;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace FPSO.HMI.Views.Dialog
{
    /// <summary>
    /// LoginDialog.xaml 的交互逻辑
    /// </summary>
    public partial class LoginDialog : Window
    {
		private DBUserInfoService g_Service = new DBUserInfoService();
		public LoginDialog()
        {
            InitializeComponent();
            // 便于测试，默认填写普通用户
            UsernameTextBox.Text = string.Empty;
            PasswordTextBox.Password = string.Empty;
        }

        /// <summary>
        /// 登录逻辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            // 获取用户输入的用户名和密码
            string username = UsernameTextBox.Text;
            string password = PasswordTextBox.Password;
            // 基本格式校验
            if (string.IsNullOrWhiteSpace(username))
            {
                MessageBox.Show("用户名不能为空！", "校验失败");
                return;
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show("密码不能为空！", "校验失败");
                return;
            }
            if (password.Length < 8)
            {
                MessageBox.Show("密码长度不小于8位！", "校验失败");
                return;
            }
			UserInfo login_user = g_Service.GetUserInfo(username);
			bool login_check = false;
			if(login_user != null)
			{
				string db_password = login_user.Password;
				string real_password = AuthUtil.DecryptStringFromBytes_Aes(db_password);
				if(password == real_password)
				{
					int userTypeValue = login_user.UserType;
					AuthUtil.SetLoginStatus(userTypeValue);
					AuthUtil.SetLoginUser(username);
					login_check = true;
					//登录成功后，加入检查密码更新时间的过程，如果密码更新时间加上7天仍早于当前时间，则表明近一周没有更新密码，提示更新密码
					DateTime updatetime = login_user.UserUpdateTime.AddDays(7);
					DateTime currenttime = DateTime.Now;
					DateTime createtime = login_user.UserCreateTime;
					int compareto = updatetime.CompareTo(currenttime);
					if(compareto == -1)
					{
						MessageBox.Show("已有超过一周未更新密码", "更新密码");
						UserEdidDialog userEdidDialog = new UserEdidDialog(1);
						userEdidDialog.ShowDialog();
					}
					this.DialogResult = true;
				}
			}
			if(login_check == false)
			{
				AlarmUtil.AddAlarmInfo("用户【" + username + "】尝试登录失败");
				MessageBox.Show("用户名或密码错误！", "登录失败");
			}
        }

        /// <summary>
        /// 退出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            //返回false以阻止后续流程并退出 
            this.DialogResult = false;
        }

        /// <summary>
        /// 密码输入框绑定回车键事件，触发登录操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PasswordTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LoginButton_Click(sender, e);
            }
        }


        /// <summary>
        /// 窗口拖动事件
        /// </summary>
        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                DragMove();
            }
        }


    }
}
